﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.Histograms;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class HistogramRelated : BaseImageFilter
    {
        protected bool m_bCreateMonochromeMask = false;

        /// <summary>
        /// Initializes a new instance of the <see cref="HistogramRelated"/> class.
        /// </summary>
        public HistogramRelated()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="HistogramRelated"/> class.
        /// </summary>
        /// <param name="_histogramRelated">The _histogram related.</param>
        internal HistogramRelated(HistogramRelated _histogramRelated)
        {
            m_bCreateMonochromeMask = _histogramRelated.m_bCreateMonochromeMask;
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            Statistics histogram = new Statistics(_bitmap);

            UnsafeBitmap bitmap = _bitmap;

            List<Color> aDict = new List<Color>();

            Color colorToSet = Color.Transparent;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            for (int y = 0; y < nHeight; y++)
            {
                for (int x = 1; x < nWidth; x++)
                {
                    Color color = bitmap.GetPixel(x, y);
                    foreach (StatisticValue value in histogram.Data)
                    {
                        if (value.Color == color)
                        {
                            continue;
                        }
                        if (ColorFunctions.ChannelRatioEqual(value.Color, color))
                        {
                            colorToSet = m_bCreateMonochromeMask ? Definitions.White : value.Color;
                            break;
                        }
                        else
                        {
                            colorToSet = m_bCreateMonochromeMask ? Definitions.Black : color;
                            break;
                        }
                    }
                    bitmap.SetPixel(x, y, colorToSet);
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Gets or sets MonochromeMask
        /// </summary>
        public bool CreateMonochromeMask
        {
            get { return m_bCreateMonochromeMask; }
            set { m_bCreateMonochromeMask = value; }
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new HistogramRelated(this);
        }
    }
}
